package com.inspection.support;

import com.inspection.constant.SortConstant;
import com.inspection.model.BasePageQueryDTO;
import org.springframework.data.domain.Sort;
import org.springframework.util.StringUtils;

import java.util.Objects;

/**
 * @author zhaoqi
 * @since 2020-12-16
 */
public class SortHelper {


    private static  Sort defaultSort;


    static {
        defaultSort = new Sort(Sort.Direction.DESC, "id");
    }


    public static Sort buildSort(BasePageQueryDTO pageQuery) {
        if (StringUtils.isEmpty(pageQuery.getSortName())) {
            return defaultSort;
        }
        Sort.Direction sortOrder = Objects.isNull(pageQuery.getSortOrder()) ?
                Sort.Direction.ASC : SortConstant.get(pageQuery.getSortOrder()).getJpaSupport();

        return new Sort(sortOrder, pageQuery.getSortName());
    }
}
